class Solution {
    class  TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while(root!=null){
            if(root.val<p.val&&root.val<q.val){
                //说明p q都在右子树
                root=root.right;
            }else if(root.val>p.val&&root.val>q.val){
                //说明 p q都在左子树
                root=root.left;
            }else{
                break;
            }
        }
        return root;
    }
}